#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>

#include "gs_softkey.h"

#define IDC_BUTTON  500


static void button_callback (HWND hWnd, int id, int nc, DWORD add_data)
{

    if (nc == BN_CLICKED) {
        int index;
        index = id - IDC_BUTTON;
        NotifyParentEx (GetParent(hWnd), GetDlgCtrlID(hWnd), GS_SKN_CLICKED,(DWORD) &index);
    }
}

static int GsSoftkeyProc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    HWND hButton;

    switch (message) 
    {
        case MSG_CREATE:
        {
            int x, y, w, h;
            int inner = 5;

            w = h = 50;

            x = 0 * w + 1 * inner;
            y = 0 * h + 1 * inner;
            hButton = CreateWindow(CTRL_BUTTON, "1", WS_VISIBLE | BS_PUSHBUTTON,
                    IDC_BUTTON + 1,
                    x, y, w, h, 
                    hWnd, (DWORD)0);
            SetNotificationCallback(hButton, button_callback);

            x = 1 * w + 2 * inner; 
            y = y;
            hButton = CreateWindow(CTRL_BUTTON, "2", WS_VISIBLE | BS_PUSHBUTTON,
                    IDC_BUTTON + 2,
                    x, y, w, h, 
                    hWnd, (DWORD)0);
            SetNotificationCallback(hButton, button_callback);
            
            x = 2 * w + + 3 * inner;
            y = y;
            hButton = CreateWindow(CTRL_BUTTON, "3", WS_VISIBLE | BS_PUSHBUTTON,
                    IDC_BUTTON + 3,
                    x, y, w, h, 
                    hWnd, (DWORD)0);
            SetNotificationCallback(hButton, button_callback);
            
            x = 0 * w + 1 * inner;
            y = 1 * h + 2 * inner;
            hButton = CreateWindow(CTRL_BUTTON, "4", WS_VISIBLE | BS_PUSHBUTTON,
                    IDC_BUTTON + 4,
                    x, y, w, h, 
                    hWnd, (DWORD)0);
            SetNotificationCallback(hButton, button_callback);
            
            x = 1 * w + 2 * inner; 
            y = y;
            hButton = CreateWindow(CTRL_BUTTON, "5", WS_VISIBLE | BS_PUSHBUTTON,
                    IDC_BUTTON + 5,
                    x, y, w, h, 
                    hWnd, (DWORD)0);
            SetNotificationCallback(hButton, button_callback);
            
            x = 2 * w + + 3 * inner;
            y = y;
            hButton = CreateWindow(CTRL_BUTTON, "6", WS_VISIBLE | BS_PUSHBUTTON,
                    IDC_BUTTON + 6,
                    x, y, w, h, 
                    hWnd, (DWORD)0);
            SetNotificationCallback(hButton, button_callback);
            
            x = 0 * w + 1 * inner;
            y = 2 * h + 3 * inner;
            hButton = CreateWindow(CTRL_BUTTON, "7", WS_VISIBLE | BS_PUSHBUTTON,
                    IDC_BUTTON + 7,
                    x, y, w, h, 
                    hWnd, (DWORD)0);
            SetNotificationCallback(hButton, button_callback);
            
            x = 1 * w + 2 * inner; 
            y = y;
            hButton = CreateWindow(CTRL_BUTTON, "8", WS_VISIBLE | BS_PUSHBUTTON,
                    IDC_BUTTON + 8,
                    x, y, w, h, 
                    hWnd, (DWORD)0);
            SetNotificationCallback(hButton, button_callback);
            
            x = 2 * w + + 3 * inner;
            y = y;
            hButton = CreateWindow(CTRL_BUTTON, "9", WS_VISIBLE | BS_PUSHBUTTON,
                    IDC_BUTTON + 9,
                    x, y, w, h, 
                    hWnd, (DWORD)0);
            SetNotificationCallback(hButton, button_callback);
            
            x = 0 * w + 1 * inner;
            y = 3 * h + 4 * inner;
            hButton = CreateWindow(CTRL_BUTTON, ".", WS_VISIBLE | BS_PUSHBUTTON,
                    IDC_BUTTON + 10,
                    x, y, w, h, 
                    hWnd, (DWORD)0);
            SetNotificationCallback(hButton, button_callback);
            
            x = 1 * w + 2 * inner; 
            y = y;
            hButton = CreateWindow(CTRL_BUTTON, "0", WS_VISIBLE | BS_PUSHBUTTON,
                    IDC_BUTTON + 0,
                    x, y, w, h, 
                    hWnd, (DWORD)0);
            SetNotificationCallback(hButton, button_callback);
            
            x = 2 * w + + 3 * inner;
            y = y;
            hButton = CreateWindow(CTRL_BUTTON, "+/-", WS_VISIBLE | BS_PUSHBUTTON,
                    IDC_BUTTON + 11,
                    x, y, w, h, 
                    hWnd, (DWORD)0);
            SetNotificationCallback(hButton, button_callback);

            return 0;
        }
    }

    return DefaultControlProc (hWnd, message, wParam, lParam);
}

BOOL RegisterGsSoftkey(void)
{
    WNDCLASS WndClass;

    WndClass.spClassName = GS_SOFTKEY;
    WndClass.dwStyle     = WS_NONE;
    WndClass.dwExStyle   = WS_EX_NONE;
    WndClass.hCursor     = GetSystemCursor (IDC_ARROW);
    WndClass.iBkColor    = COLOR_lightwhite;
    WndClass.WinProc     = GsSoftkeyProc;

    return RegisterWindowClass (&WndClass);
}

void UnregisterGsSoftkey (void)
{
    UnregisterWindowClass (GS_SOFTKEY);
}


